package com.wms.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wms.bean.PageBean;
import com.wms.code.MyCodeFactory;
import com.wms.dto.ArrivalDto;
import com.wms.entity.Arrival;
import com.wms.mapper.ArrivalDetailMapper;
import com.wms.mapper.ArrivalMapper;
import com.wms.mapper.ArrivalQualityInspectionMapper;
import com.wms.mapper.BatchMapper;
import com.wms.service.ArrivalService;
import com.wms.utils.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class ArrivalServiceImpl implements ArrivalService {
    @Autowired
    ArrivalMapper arrivalMapper;

    @Autowired
    ArrivalDetailMapper arrivalDetailMapper;

    @Autowired
    BatchMapper batchMapper;

    @Autowired
    ArrivalQualityInspectionMapper arrivalQualityInspectionMapper;

    //最大到货单号
    private String maxArrivalCode;

    //最大批次号
    private String maxBatchCode;

    @Transactional
    @Override
    public int save(Arrival arrival) {
        //1.生成到货单号
        //查询最大编码
        maxArrivalCode =arrivalMapper.getMaxCode();
        //根据最大编码生成新的编码
        maxArrivalCode = MyCodeFactory.createArrivalCode(maxArrivalCode);
        arrival.setArrivalCode(maxArrivalCode);
        //2.保存到货单数据 要求返回生成的id
        int rs=arrivalMapper.saveArrival(arrival);
        Assert.isZero(rs,"保存到货单失败");
        int arrivalId=arrival.getId();
        //3.自动生成批次号 在b_batch 中根据到货明细保存记录 只要保存批次编号 物料id
        //生成批次号
        if(null==maxBatchCode){
            maxBatchCode = batchMapper.getMaxCode();
            //根据最大编码生成新的编码
            maxBatchCode= MyCodeFactory.createBatchCode(maxBatchCode);
        }
        List<Integer> ids=new ArrayList<>();
        //遍历明细
        arrival.getArrivalDetailList().forEach(el->{
            ids.add(el.getMid());
        });
        //保存批次
        int brs=batchMapper.setBatchNo(maxBatchCode,ids);
        Assert.isZero(brs,"保存批次失败");
        //insert into b_batch(batcnNo,mid)
        //4.保存到货明细  批量插入
        int drs=arrivalDetailMapper.saveArrivalDetail(arrivalId, maxBatchCode,arrival.getArrivalDetailList());
        Assert.isZero(drs,"保存到货明细失败");
        //5.在b_arrival_quality_inspection 到货质检表中 插入数据只记录 到货单id 明细id insert into() select
        int qrs=arrivalQualityInspectionMapper.add(arrivalId);
        Assert.isZero(qrs,"保存质检信息失败");
        //6.生成采购入库任务单。调用采购入库任务单接口 ，生成入库任务单
        return 1;
    }

    @Override
    public PageBean queryAll(ArrivalDto arrivalDto) {
        PageHelper.startPage(arrivalDto.getPage(),arrivalDto.getPageSize());
        List<Map> list=arrivalMapper.selectAll(arrivalDto);
        PageInfo info=new PageInfo(list);
        return new PageBean<>(arrivalDto.getPage(),arrivalDto.getPageSize(),info.getTotal(),list);
    }

    @Override
    public List<Arrival> queryDetailById(int id) {
        return arrivalMapper.selectDetailById(id);
    }

    @Override
    public int removeById(int id) {
        return arrivalMapper.deleteById(id);
    }


}
